#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    : LoginGraphicEngine.py
@Desc    : 
@Version : 1.0
@Author  : echohelper 
@Email   : echohelper@qq.com
@WebSite : www.github.com/echohelper
@Time    : 2021/10/15 17:35:43
'''
# Start typing your code from here

import os
import requests
import json
from urllib.parse import urlencode, quote, unquote


class LoginGraphicEngine:
    token = ''
    svrAddr = ''
    loginAddr = ''

    def __init__(self) -> None:
        pass

    def initAddr(self, svrAddr:str, loginAddr:str) ->None:
        LoginGraphicEngine.svrAddr = svrAddr
        LoginGraphicEngine.loginAddr = loginAddr

    def login(self, addr:str, data:str) -> bool:
        url = addr + '/api/user/login'
        print('url:' + url)
        print('body:' + data)
        
        headers = {
            'Content-type':'application/json',
            'User-Agent':'BimWorker/1.0.0.0 (winnt; 10.0.18363)'
        }

        # get a new url that include code
        rData = requests.post(url, data = data, headers = headers)
        obj = json.loads(rData.text)
        print(obj['data'])
        print(obj)
        bRet = obj['code'] == 200

        if bRet:
            codeUrl = obj['data']
            idx = codeUrl.index('=', 0, len(codeUrl))
            code =codeUrl[idx+1:]

            newUrl = addr + '/api/user/token/' + code
            rData = requests.get(newUrl)
            obj = json.loads(rData.text)
            print(obj)
            bRet = obj['code'] == 200
            if bRet:
                LoginGraphicEngine.token = obj['data']['access_token']
            else:
                LoginGraphicEngine.token = ''
                return False
        
        print(LoginGraphicEngine.token)

        return bRet

    def getUploadToken(self, filePath:str) -> str:
        url = LoginGraphicEngine.svrAddr + '/api/v1/oss/access/model'
        print('url:' + url)
        print('filePath:' + filePath)
        
        headers = {
            'Content-type':'application/json',
            'x-access-token':LoginGraphicEngine.token,
            'Cookie':'SECKEY_CID=72b36e065ee90b3906dcf12017f317b01baf090e; BMAP_SECKEY=cde6ebb241c3d75c675c8688828640edba33c570fc006f6ccdee864f2e95d88033fc19e794fee19c2417a6953ba260f3e91efa7e82cbc9c45b5854aec79ce924a65376227f1f9b41201f41aba666e7dc0b85b18743da9834b514f123c1bd29801d06e88d9b3a5ba1a52143c58847defeba027068008f990a4fac21f4e39b25ec3d4f2bc4f59113393f3ebc9330c375df13092b99e173da2eddd6e597eaca7e262992f151f1957c148721e75eaa9ff9cd73d047922238d764dfb0835eb9394025132f74aa50c460c6c31a5abfcc39ec27722538431004451f07ed0bad86eee54727e9aa3a37d5ef2ca598c607749a80e5',
            'User-Agent':'BimWorker/1.0.0.0 (winnt; 10.0.18363)'
        }

        fileSize = os.path.getsize(filePath)
        fileSuffix = os.path.splitext(filePath)[-1]
        data = {}
        data['fileSize'] = fileSize
        data['fileType'] = fileSuffix
        data['operation'] = 'upload'

        rData = requests.post(url, data=json.dumps(data), headers = headers)
        obj = json.loads(rData.text)
        print(obj)
        if obj['code'] == 200:
            return obj['data']
        return ''

    def getDownloadToken(self) -> str:
        url = LoginGraphicEngine.svrAddr + '/api/v3/model/file/download/616fcf0e84ab7a3680f995ff/1'
        print('url:' + url)
        
        headers = {
            'Content-type':'application/json',
            'x-access-token':LoginGraphicEngine.token,
            'Cookie':'SECKEY_CID=72b36e065ee90b3906dcf12017f317b01baf090e; BMAP_SECKEY=cde6ebb241c3d75c675c8688828640edba33c570fc006f6ccdee864f2e95d88033fc19e794fee19c2417a6953ba260f3e91efa7e82cbc9c45b5854aec79ce924a65376227f1f9b41201f41aba666e7dc0b85b18743da9834b514f123c1bd29801d06e88d9b3a5ba1a52143c58847defeba027068008f990a4fac21f4e39b25ec3d4f2bc4f59113393f3ebc9330c375df13092b99e173da2eddd6e597eaca7e262992f151f1957c148721e75eaa9ff9cd73d047922238d764dfb0835eb9394025132f74aa50c460c6c31a5abfcc39ec27722538431004451f07ed0bad86eee54727e9aa3a37d5ef2ca598c607749a80e5',
            'User-Agent':'BimWorker/1.0.0.0 (winnt; 10.0.18363)'
        }

        rData = requests.get(url, headers = headers)
        obj = json.loads(rData.text)
        print(obj)
        if obj['code'] == 200:
            codeUrl = obj['data']
            idx = codeUrl.index('=', 0, len(codeUrl))
            token =codeUrl[idx+1:]
            return token

        return ''

    def uploadModelFile(self, filePath:str) -> bool:
        access = self.getUploadToken(filePath)
        if access == '':
            return False

        url = LoginGraphicEngine.svrAddr + '/api/v4/file/model/upload?token=' + access
        print('url:' + url)
        headers = {
            'Accept':'application/json, text/plain, */*',
            'X-Requested-With':'XMLHttpRequest',
            'Content-type':'multipart/form-data; boundary=----WebKitFormBoundaryzC5tjBDoW2WBwzfA',
            'Cookie':'SECKEY_CID=72b36e065ee90b3906dcf12017f317b01baf090e; BMAP_SECKEY=cde6ebb241c3d75c675c8688828640edba33c570fc006f6ccdee864f2e95d88033fc19e794fee19c2417a6953ba260f3e91efa7e82cbc9c45b5854aec79ce924a65376227f1f9b41201f41aba666e7dc0b85b18743da9834b514f123c1bd29801d06e88d9b3a5ba1a52143c58847defeba027068008f990a4fac21f4e39b25ec3d4f2bc4f59113393f3ebc9330c375df13092b99e173da2eddd6e597eaca7e262992f151f1957c148721e75eaa9ff9cd73d047922238d764dfb0835eb9394025132f74aa50c460c6c31a5abfcc39ec27722538431004451f07ed0bad86eee54727e9aa3a37d5ef2ca598c607749a80e5',
            'x-access-token':LoginGraphicEngine.token,
            'User-Agent':'BimWorker/1.0.0.0 (winnt; 10.0.18363)'
        }

        fileName = os.path.basename(filePath)
        files = {
            'file1': (fileName, 'application/octet-stream', open(filePath, 'rb'))
        }

        rData = requests.post(url, headers = headers, files=files)
        obj = json.loads(rData.text)
        print(obj)

    def downloadModelFile(self, filePath:str) -> bool:
        downloadToken = self.getDownloadToken()
        print(downloadToken)
        url = LoginGraphicEngine.svrAddr + '/api/v4/file/download?token=' + downloadToken
        print('url:' + url)
        
        headers = {
            'Content-type':'application/json',
            'x-access-token':LoginGraphicEngine.token,
            'Cookie':'SECKEY_CID=72b36e065ee90b3906dcf12017f317b01baf090e; BMAP_SECKEY=cde6ebb241c3d75c675c8688828640edba33c570fc006f6ccdee864f2e95d88033fc19e794fee19c2417a6953ba260f3e91efa7e82cbc9c45b5854aec79ce924a65376227f1f9b41201f41aba666e7dc0b85b18743da9834b514f123c1bd29801d06e88d9b3a5ba1a52143c58847defeba027068008f990a4fac21f4e39b25ec3d4f2bc4f59113393f3ebc9330c375df13092b99e173da2eddd6e597eaca7e262992f151f1957c148721e75eaa9ff9cd73d047922238d764dfb0835eb9394025132f74aa50c460c6c31a5abfcc39ec27722538431004451f07ed0bad86eee54727e9aa3a37d5ef2ca598c607749a80e5',
            'User-Agent':'BimWorker/1.0.0.0 (winnt; 10.0.18363)'
        }

        rData = requests.get(url, headers = headers)
        cdData = rData.headers['Content-Disposition']
        idx = cdData.index('=', 0, len(cdData))
        fileName = unquote(cdData[idx+1:])
        with open(filePath + fileName, "wb") as code:
            code.write(rData.content)

        return True

        